use kuchiki::traits::*;

pub fn flatten(html: &str) -> String {
    let tree = kuchiki::parse_html().one(html);
    let mut result = String::default();
    for paragraph in tree.descendants() {
        let node = paragraph;
        if let Some(element) = node.as_element() {
            if &element.name.local == "p" {
                if let Some(text) = node.as_text() {
                    result += text.borrow().as_str();
                }
                result += "\n\n";
                continue;
            }
            if &element.name.local == "br" {
                result += "\n";
                continue;
            }
        }
        if let Some(text) = node.as_text() {
            result += text.borrow().as_str();
        }
    }
    return result.trim().to_string();
}
